/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package communication;

import flexjson.JSONDeserializer;
import flexjson.JSONException;
import flexjson.JSONSerializer;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import model.Control;
/**
 *
 * @author Fernando
 */
public class QControl extends RequestServer {

    private String table_name = "control";
    private String json = "";
    private JSONArray array;
    private ArrayList<JSONObject> objList = new ArrayList<JSONObject>();
    private Integer idControl = null;
    private String controlComment = null;

    public static void main(String[] args) throws IOException, JSONException, UnsupportedEncodingException, org.codehaus.jettison.json.JSONException {
        //USAGE
        /*GET ALL REGISTERS
        QControl SV = new QControl();
        
        ArrayList<Control> All = SV.GetAll();
        
        for (Control V : All){
            
            System.out.println(V.getControlComment());        
        }
        //*/
        
        /*GET ONE REGISTER
        
         QControl SV = new QControl(2);
        
         //OR: QVaccine SV = new QVaccine(name); 
         
         Control V = SV.GetObject(0); // index 0, the first returned value
        //*/
        
        /*DELETE
        QControl QV = new QControl();
        
        String R = QV.Delete(null, null);
        //*/
        
        /*UPDATE
        QControl QC = new QControl();
        
        String R = QC.Update(2, "novonome");
        //*/
        
        /*INSERT 
        QControl QC = new QControl();
        
        String answer = QC.Insert("newco233lqkwma");
       //*/
    }
    public JSONObject Serialize(Control C) throws JSONException, org.codehaus.jettison.json.JSONException{
        
        this.json = new JSONSerializer().serialize(C);        
        JSONObject obj = new JSONObject(this.json);
        
        obj.remove("class");  
        return obj;
    }    
    public String Insert(String controlComment) throws UnsupportedEncodingException, IOException, JSONException, org.codehaus.jettison.json.JSONException{
        
        Control C = new Control(controlComment);
        
        JSONObject obj = this.Serialize(C);
        
        obj.remove("idControl");
        
        String post = this.post("["+obj.toString()+"]", this.table_name);
        
        return post;
    }
    
    public String Update(int id, String control) throws UnsupportedEncodingException, IOException, JSONException, org.codehaus.jettison.json.JSONException{
        
        Control C = new Control(id, control);
        this.idControl = id;
        
        JSONObject obj = this.Serialize(C);
        
        this.json = obj.toString();
        
        String parameters = PrepareParameters();
        
        System.out.println(this.json);
        System.out.println(parameters);
        
        String put = this.put(this.table_name, this.json, parameters);
        
        return put;
    }
    
    public String Delete(Integer id, String control) throws IOException{
        
        this.idControl = id;
        this.controlComment = control;
        
        String delete = this.delete(this.table_name, PrepareParameters());
        
        return delete;
    }
    
    private boolean  SetJsonList() throws JSONException, org.codehaus.jettison.json.JSONException {
        //prepare list of json objects
        
        if ("".equals(this.json)){
            return false;
        }
        
        this.array = new JSONArray(this.json);

        for (int i = 0; i < array.length(); i++) {

            JSONObject tmp = array.getJSONObject(i);
            objList.add(tmp);
        }
        return true;
    }

    public Control GetObject(int index) throws JSONException, org.codehaus.jettison.json.JSONException {

        if (this.json == ""){
            Control C = new Control();
            return C;
        }
        
        this.array = new JSONArray(this.json);

        JSONObject obj = null;

        obj = objList.get(index);

        obj.put("class", Control.class.getCanonicalName());

        String djson = obj.toString();

        Control C = new JSONDeserializer<Control>().deserialize(djson);

        return C;
    }

    public ArrayList<Control> GetAll() throws JSONException, IOException, org.codehaus.jettison.json.JSONException {
        
        this.json = this.get(this.table_name, null);
        SetJsonList();
        
        ArrayList<Control> Varray = new ArrayList<Control>();
        
        for(int i=0; i<objList.size();i++){
            
            Varray.add(GetObject(i));
        }
        return Varray;
    }

    public String PrepareParameters() {

        String control = "";
        String id = "";
        String ret = "";
        
        id = (this.idControl != null) ? ("idControl="+this.idControl) : "";
        control = (this.controlComment != null) ? ("controlComment="+this.controlComment) : "";
        
        if (!"".equals(id) || !"".equals(control)){
            return "?"+id+"&"+control;
        }
        return ret;
    }

    public QControl(Integer idControl) throws IOException, JSONException, org.codehaus.jettison.json.JSONException {

        super();

        if (idControl != null) {

            this.idControl = idControl;
            this.json = this.get(this.table_name, this.PrepareParameters());
        } else {
            this.json = this.get(this.table_name, null);
        }
        SetJsonList();
    }

    public QControl(String controlComment) throws IOException, JSONException, org.codehaus.jettison.json.JSONException {
        super();

        if (controlComment != null) {

            this.controlComment = controlComment;
            this.json = this.get(this.table_name, this.PrepareParameters());
        } else {
            this.json = this.get(this.table_name, null);
        }
        SetJsonList();
    }

    public QControl() throws IOException, JSONException {
        super();
    }

    public JSONArray getArray() {
        return array;
    }

    public void setArray(JSONArray array) {
        this.array = array;
    }

    public String getControlComment() {
        return controlComment;
    }

    public void setControlComment(String controlComment) {
        this.controlComment = controlComment;
    }

    public Integer getIdControl() {
        return idControl;
    }

    public void setIdControl(Integer idControl) {
        this.idControl = idControl;
    }

    public String getJson() {
        return json;
    }

    public void setJson(String json) {
        this.json = json;
    }

    public ArrayList<JSONObject> getObjList() {
        return objList;
    }

    public void setObjList(ArrayList<JSONObject> objList) {
        this.objList = objList;
    }

    public String getTable_name() {
        return table_name;
    }

    public void setTable_name(String table_name) {
        this.table_name = table_name;
    }

    
}
